/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package productorconsumidorpetri;

public class Buffer 
{
    private int [] buffer;
    private int elementos;
    
    public Buffer(int capacidad) 
    {
        buffer = new int [capacidad];
        for (int i=0 ; i<capacidad;i++)
        {   buffer[i] = 0;  }
        elementos=0;
    }

    synchronized public void poner(int valor)
    {   	
    	if(elementos==5)
    	{
    		System.err.println( "ErrrrorrrrERRRRRORRRERRRRRORRRERRRRRORRRERRRRRORRRERRRRRORRRERRRRRORRRrRRRR PONER ELEMENTO 5!!!!!" );
    	}
    	buffer[elementos] = valor;  
    	elementos++;
        //System.out.println( Thread.currentThread().getName() + " escribe " + valor );
        System.out.println("Buffer: {" + buffer[0] + " ; " + buffer[1] + " ; "+ buffer[2] + " ; "+ buffer[3] + " ; "+ buffer[4] + " } ");
    }
	
    synchronized public int sacar()
    {
    	if(elementos==0)
    	{
    		System.err.println( "ERRRRRRROERRRRRORRRERRRRRORRRERRRRRORRRERRRRRORRRERRRRRORRRERRRRRORRRERRRRRORRRRRRR LEER ELEMENTO -1!!!!!" );
    	}
    	
        int valor_leido = buffer[elementos-1];
        buffer[elementos-1] = 0;
        elementos--;   
        //System.out.println( Thread.currentThread().getName() + " lee " + valor_leido );
        System.out.println("Buffer: {" + buffer[0] + " ; " + buffer[1] + " ; "+ buffer[2] + " ; "+ buffer[3] + " ; "+ buffer[4] + " } ");
        return valor_leido;
    }
    
    public int elementos()
    {
    	return elementos;
    }
}